package de.revdev.llt.module.useradministrtaion;

import de.revdev.llt.module.utility.MessageProvider;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author f.lindermaier
 */
@Named(value = "loginBean")
@RequestScoped
public class LoginBean {

    private MessageProvider provider;
    private UserFacade uf;
    private HttpServletRequest request;
    private UserBean userBean;
    private String username;
    private String password;
    private User user;
    private static final Logger log = Logger.getLogger(LoginBean.class.getName());

    /**
     * Creates a new instance of LoginBean
     */
    public LoginBean() {
    }

    public String login() {
        try {
            
            // Wenn der Login nicht funktioniert gibt es eine ServletException
            request.login(username, password);
            
            // Wenn der Benutzer nicht gefunden wird, dann gibt es eine NoResultException
            user = uf.findByEmail(username);
            
            userBean.setUser(user);
            log.log(Level.INFO, "User {0} is Logedin", username);
            return "/faces/user/home.xhtml?redirect=true";
        } catch (ServletException se) {
            provider.addMessage(null, FacesMessage.SEVERITY_ERROR, "userBean_login_failed");
            log.log(Level.WARNING, "Userlogin failed for {0}", username);
            return "";
        } catch (Exception e) {
            provider.addMessage(null, FacesMessage.SEVERITY_ERROR, "userBean_login_failed");
            log.log(Level.WARNING, "Database access failed for {0}", username);
            return "";
        }
    }

    public String logout() {
        try {
            user = userBean.getUser();
            request.logout();
            request.getSession().invalidate();
            log.log(Level.INFO, "{0} logged out", user.getEmail());
        } catch (ServletException ex) {
            log.log(Level.SEVERE, "Logout failed for " + user.getEmail(), ex);
        }
        return "/faces/index.xhtml?redirect=true";
    }

    @Inject
    public void setMeassageProvider(MessageProvider messageProvider) {
        this.provider = messageProvider;
    }

    @Inject
    public void setUserFacade(UserFacade userFacade) {
        this.uf = userFacade;
    }

    @Inject
    public void setRequest(HttpServletRequest httpServletRequest) {
        this.request = httpServletRequest;
    }

    @Inject
    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
